﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vds2465Telegrams;

namespace Vds2465Parser
{
    internal static class GenParse_ReportType_Intrusion
    {
        #region private constants

        internal const byte MsgBlockOnlyValue = 0x20;

        private const byte GeneralByte = 0x00;
        private const byte AssaultByte = 0x01;
        private const byte BurglaryByte = 0x02;
        private const byte SabotageByte = 0x03;
        private const byte HostageTakingByte = 0x04;
        private const byte AreaByte = 0x0F;

        #endregion private constants

        #region internal methods

        internal static byte GenerateByte(ReportType_Intrusion reportTypeIntrusion)
        {
            byte resultByte = 0x0;

            // Intrusion has 0x1 in Bits 6,5,4
            resultByte += MsgBlockOnlyValue;

            // add the report type sub type
            if (reportTypeIntrusion.ReportSubType == eReportSubType_Intrusion.General)
            {
                resultByte |= GeneralByte;
                return resultByte;
            }

            if (reportTypeIntrusion.ReportSubType == eReportSubType_Intrusion.Assault)
            {
                resultByte |= AssaultByte;
                return resultByte;
            }

            if (reportTypeIntrusion.ReportSubType == eReportSubType_Intrusion.Burglary)
            {
                resultByte |= BurglaryByte;
                return resultByte;
            }

            if (reportTypeIntrusion.ReportSubType == eReportSubType_Intrusion.Sabotage)
            {
                resultByte |= SabotageByte;
                return resultByte;
            }

            if (reportTypeIntrusion.ReportSubType == eReportSubType_Intrusion.HostageTaking)
            {
                resultByte |= HostageTakingByte;
                return resultByte;
            }

            if (reportTypeIntrusion.ReportSubType == eReportSubType_Intrusion.Area)
            {
                resultByte |= AreaByte;
                return resultByte;
            }

            // TODO handle error (unknown eReportSubType_Intrusion)
            return resultByte;
        }

        internal static bool ParseByte(byte byteValue, out eReportSubType_Intrusion reportSubTypeIntrusion)
        {
            // set arbitary default value
            reportSubTypeIntrusion = eReportSubType_Intrusion.General;

            // get only bits 6,5,4
            byte messageBlock = (byte)(byteValue & (byte)0x70);
            if (messageBlock == MsgBlockOnlyValue)
            {
                // get only bits 3,2,1,0
                byte subType = (byte)(byteValue & (byte)0x0f);
                if (subType == GeneralByte)
                {
                    reportSubTypeIntrusion = eReportSubType_Intrusion.General;
                    return true;
                }

                if (subType == AssaultByte)
                {
                    reportSubTypeIntrusion = eReportSubType_Intrusion.Assault;
                    return true;
                }

                if (subType == BurglaryByte)
                {
                    reportSubTypeIntrusion = eReportSubType_Intrusion.Burglary;
                    return true;
                }

                if (subType == SabotageByte)
                {
                    reportSubTypeIntrusion = eReportSubType_Intrusion.Sabotage;
                    return true;
                }

                if (subType == HostageTakingByte)
                {
                    reportSubTypeIntrusion = eReportSubType_Intrusion.HostageTaking;
                    return true;
                }

                if (subType == AreaByte)
                {
                    reportSubTypeIntrusion = eReportSubType_Intrusion.Area;
                    return true;
                }

                // bits 3,2,1,0 are wrong
                return false;
            }

            // bits 6,5,4 are wrong
            return false;
        }

        #endregion internal methods
    }
}
